<?php
/* MyMeeting is an online meeting management software.
 * Copyright (C) 2009 Government Of Malaysia
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * @author: Abdullah Zainul Abidin, Nuhaa All Bakry
 *          Eavay Javay Barnad, Sarogini Muniyandi, Teow Jit Huan
 *
 */

class MultiFileComponent extends Object
{
/**
 * Define $controller
 *
 */
    var $controller;
/**
 * Define $args
 *
 */
    var $args=array('action'=>null);

/**
 * Describe startup
 *
 * @param $controller
 * @return null
 */
    function startup(&$controller){
        $args=$controller->passedArgs;
        $this->controller=$controller;
        if($controller->action=="attachment"){
            if($args[1]){
                return $this->downloadfile($args[1]);
            }
        }
    }

/**
 * Describe downloadfile
 *
 * @param $id
 * @return null
 */
    function downloadfile($id){
        $this->mfile=ClassRegistry::init('Attachment','model');
        $dfile=$this->mfile->find('first',array('conditions'=>array('id' => $id)));
        App::import('Component', 'Session');
        $Session = new SessionComponent(); 
        $user = $Session->read('Auth.User'); 
        //check whether the current user have permission to access the file
        if($this->mfile->viewPermission($dfile,$user['id'])==0){
            $Session->setFlash(__('You don\'t have permission to view this file', true));
            $url=$Session->read('history');
            if($url=='getmultidata'){
                $url='committees/find';
            }
            $this->controller->redirect('/'.$url);
        }
        elseif($dfile){
            header("Last-Modified: ".$dfile['Attachment']['modified']);
            header("Content-Type: " .$dfile['Attachment']['type']);
            header("Content-Length: " .$dfile['Attachment']['size']);
            header("Content-disposition: attachment; filename=\"".$dfile['Attachment']['filename']."\"");
            readfile($dfile['Attachment']['file']);
            exit();
        }
        else{
            return false;
        }
    }
}
?>
